package bo;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import vo.StackTraceVo;

public class ReasonEstimateBo {
	private final static String packageName ="tw.gov.moi.";
	
	public static  String estimateV01(final StackTraceVo vo){
		String reason =null;
		Map<String,String>  map = 	getCommonCause();
		final String stackTrace = vo.getStacktrace();
		final	String[] lines = StringUtils.split(stackTrace ,"\n\r");
		for(String line :lines ){
			for(String searchSeq :map.keySet()){
				if(StringUtils.contains(line, searchSeq)){
					 return map.get(searchSeq);
				}
			}
			 final String regExpr =".*tw.gov.moi.*.service.*";
			 if( line.contains(packageName)  && line.matches(regExpr)){
				 return extractSercvice (line);
			 }
		}
		return reason;
	}
	public static Map<String,String>  getCommonCause(){
		final Map<String,String> result = new HashMap<String, String>();
		result.put("cognos", "cognos問題");
		result.put("CharsetChunk", "IBM JDK 6與 Oracle Weblogic 處理異體字問題");
		result.put("weblogic.jms.common", "weblogic jms 處理問題");
		result.put("RcTransportImageComponentImp.getArguments", "中央影像主機存取問題");
		result.put("com_informix_jdbc", "DB主機存取問題");
		result.put("com.informix.jdbc.IfxSqli", "DB主機存取問題");
		result.put("imageserver.ImageClient.addImage", "中央影像主機新增影像異常");
		result.put("TokenHelper.processGetUsedTokenByActionSite", "底層檔案鎖定清單取得過久問題");
		return result;
	}
	public static String extractSercvice(String line){
		final   String regExpr ="(\\w*Impl)";
		final	Matcher matcher = Pattern.compile(regExpr).matcher(line);
		while (matcher.find()) {
			return matcher.group(matcher.groupCount());
		}
		return line; 
	}
}
